﻿///<reference path="_moduleReferences.ts" />

module ScrambledEggs.Modules {
    export class FirstMoves implements IModule {

        private _onUpdate: () => void;
        private _holdingSomething: bool = false;
        private _feeding: number = 0;

        constructor() {
        }

        initialize(gameState: GameStateProvider, onUpdate: () => void ) {
            var me = this;
            this._onUpdate = onUpdate;
            var moves = $("#firstmoves");

            moves.find("button.stare").click(function () {
                gameState.State.MentalMaturity++;
                $(this).hide();

                setTimeout(function () { gameState.State.Message = "."; me._onUpdate(); }, 500);
                setTimeout(function () { gameState.State.Message = ".."; me._onUpdate(); }, 1000);
                setTimeout(function () { gameState.State.Message = "..."; me._onUpdate(); }, 1500);
                setTimeout(function () {
                    gameState.State.Message = "You realised you have fingers!";
                    gameState.State.MentalMaturity += 6;
                    me._onUpdate();
                }, 3000);
            });

            moves.find("button.finger").click(function() {
                ++gameState.State.PhysicalMaturity;
                switch (gameState.State.PhysicalMaturity) {
                    case 0:
                        gameState.State.Message = "Great, you moved it!";
                        break;
                    case 1:
                        gameState.State.Message = "Hmm!";
                        break;
                    case 2:
                        gameState.State.Message = "You're getting the hang of this!";
                        break;
                    case 3:
                        gameState.State.Message = "Keep up the good work!";
                        break;
                }
                me._onUpdate();
            });

            moves.find("button.grab").click(function () {
                ++gameState.State.PhysicalMaturity;
                me._feeding = 0;
                if (Math.random() * 1000 < 650 + gameState.State.PhysicalMaturity * 2) {
                    me._holdingSomething = true;
                    gameState.State.Message = "You grab something";
                    me._onUpdate();
                }
                else {
                    me._holdingSomething = false;
                    gameState.State.Message = "You grab nothing";
                    me._onUpdate();
                }
                Cooldowns.Set("#firstmoves button.grab", 3);
            });

            moves.find("button.throw").click(function () {
                if (gameState.State.DecisionCatFood == CatFoodDecision.Reached) {
                    gameState.State.DecisionCatFood = CatFoodDecision.Threw;
                    gameState.State.Message = "You <em>threw away</em> her food bowl in revenge! The Cat <i>=°ᴥ°=</i> won't soon forget this!";
                    me._onUpdate();
                    return;
                }

                if (gameState.State.Inventory["PenKey"] == -1) {
                    gameState.State.Message = "<em>You threw it away somewhere. I hope it wasn't anything important...</em>";
                    gameState.State.Inventory["PenKey"] = 0;
                    me._onUpdate();
                    return;
                }

                var hit = <any>(Math.random()+"").substr(3, 5) % 10;
                switch (hit) {
                    case 0:
                    case 1:
                        gameState.State.Message = "You <em>almost</em> hit The Cat. <i>=°ᴥ°=</i>.";
                    case 2:
                        gameState.State.Message = "<strong>You hit grandpa. Your mucus looks good on him. :)</strong>";
                        break;
                    case 3:
                    case 4:
                        gameState.State.Message = "You hit The Cat. <i>=°ᴥ°=</i> The Cat uses Mean Look.";
                        break;
                    default:
                        gameState.State.Message = "You tossed it out of the pen.";
                        break;
                }
                gameState.State.PhysicalMaturity++;
                gameState.State.MentalMaturity++;
                me._holdingSomething = false;
                me._feeding = 0;
                me._onUpdate();
            });

            moves.find("button.eat").click(function () {
                if (gameState.State.DecisionCatFood == CatFoodDecision.Reached) {
                    gameState.State.DecisionCatFood = CatFoodDecision.Ate;
                    gameState.State.Fullness = 99;
                    gameState.State.Message = "You ate her food. The Cat <i>=°ᴥ°=</i> understands, but she's very annoyed!";
                    me._onUpdate();
                    return;
                }

                if (gameState.State.Inventory["PenKey"] == -1) {
                    gameState.State.Message = "<em>It tastes like iron and has the shape of a key.</em>";
                    gameState.State.Inventory["PenKey"] = 1;
                    me._onUpdate();
                    return;
                }

                var random = Math.floor((Math.random() * 100000)) % 5;

                switch (random) {
                    case 0:
                    case 1:
                    case 2:
                        gameState.State.Message = "It's food!";
                        gameState.State.Fullness = Utility.Number.Limit(gameState.State.Fullness + 35, 0, 100);
                        break;
                    case 3:
                        gameState.State.Message = "Tastes bad...";
                        if(gameState.State.MentalMaturity < 50)
                            gameState.State.MentalMaturity += 2;
                        break;
                    case 4:
                        gameState.State.Message = "Tastes awful =/";
                        if(gameState.State.Fullness > 0)
                            gameState.State.Fullness = ScrambledEggs.Utility.Number.Limit(gameState.State.Fullness - 5, 0, 100);
                        break;
                }

                me._holdingSomething = false;
                me._feeding = 0;
                me._onUpdate();
            });

            moves.find("button.cry").click(function () {
                var button = $(this);
                me._holdingSomething = false;
                gameState.State.Message = "You start to cry...";

                if (gameState.State.LeftPen) {
                    Cooldowns.Lock();
                    setTimeout(function () {
                        gameState.State.Message = "It's baby feeding time!";
                        me._onUpdate();
                    }, 3000);
                    setTimeout(function () {
                        gameState.State.Message = "...?";
                        me._onUpdate();
                    }, 5000);
                    setTimeout(function () {
                        gameState.State.Message = "......?";
                        me._onUpdate();
                    }, 7000);
                    setTimeout(function () {
                        gameState.State.Message = ".........?!";
                        me._onUpdate();
                    }, 8000);
                    setTimeout(function () {
                        gameState.State.Message = "!!";
                        me._onUpdate();
                    }, 10000);
                    setTimeout(function () {
                        gameState.State.Message = "The Cat <i>=°ᴥ°=</i> has closed the door, they can't hear you!";
                        me._onUpdate();
                        Cooldowns.Unlock();
                    }, 12000);
                    return;
                }

                Cooldowns.Set("#firstmoves button.cry", 40);
                Cooldowns.SetIfGreater("#firstmoves button.grab", 8);
                Cooldowns.SetIfGreater("#play button", 16);
                me._onUpdate();

                me._feeding = 70;
                setTimeout(function () {
                    gameState.State.Message = "It's baby feeding time!";
                    var interval = setInterval(function () {
                        if ((gameState.State.Fullness += 2) >= 100) {
                            gameState.State.Fullness = Math.min(gameState.State.Fullness, 99);
                            clearInterval(interval);
                        }
                        me._onUpdate();
                    }, 300);
                }, 3000);
            });
        }

        update(gameState: GameStateProvider, triggerSource: IModule): void {
            var me = this;
            var moves = $("#firstmoves");

            if (gameState.State.Phase != GamePhase.Baby || gameState.State.DecisionCatFood >= CatFoodDecision.Ate) {
                moves.hide();
                return;
            }
            moves.show();

            moves.find("button.stare").toggle(gameState.State.MentalMaturity == -1);
            moves.find("button.finger").toggle(gameState.State.MentalMaturity >= 5 && gameState.State.PhysicalMaturity <= 5);
            moves.find("button.grab").toggle(!me._holdingSomething && gameState.State.PhysicalMaturity >= 5 && gameState.State.MentalMaturity > 5 && gameState.State.Inventory["PenKey"] != -1 && gameState.State.DecisionCatFood != CatFoodDecision.Reached);
            moves.find("button.throw").toggle(me._holdingSomething || gameState.State.Inventory["PenKey"] == -1 || gameState.State.DecisionCatFood == CatFoodDecision.Reached);
            moves.find("button.eat").toggle(me._holdingSomething || gameState.State.Inventory["PenKey"] == -1 || gameState.State.DecisionCatFood == CatFoodDecision.Reached);
            moves.find("button.cry").toggle(gameState.State.Fullness >= 0 && gameState.State.Inventory["PenKey"] != -1 && gameState.State.DecisionCatFood != CatFoodDecision.Reached);

            if (triggerSource != me) {
                me._feeding = 0;
            }
        }
    }
}